*Summary statistics

***
***
*Figure 1
use "${mastersi}\si_combined0112.dta", clear

gen pdty = value_added/num_workers

drop if pdty==. | prod3 ==.

gen prod2 = floor(prod3/10)

egen pdtyBin10 = xtile (pdty), by(prod2) nq(10)

gen LaborIntensity = wages/ value_added

collapse (mean) LaborIntensity, by(prod2 pdtyBin10)

reshape wide LaborIntensity, i(prod2) j(pdtyBin10)  

export excel using "${figs}fig1", replace


***
***
*Figure 2 and Figure 3
use "${masterweather}temp_daily.dta", clear

sort _id

ren _id _ID

merge m:1 _ID using "${otherdata}ID2Kabucode90.dta"

drop _m

label var t2m "Average Air Temperature At 2 m Above The Surface Of The Earth (degrees C)"
label var weday "Day of the year"

destring PKABCODE, replace

*MERGE WITH LAT/LONG DATA BY KABU
sort _ID

merge m:1 _ID using "${otherdata}Kapu_coormean.dta" 

drop _merge

*for missing values in NASA data, replace -99 with .
replace rain = . if rain == -99
replace t2m = . if t2m == -99
replace tmax = . if tmax == -99 
replace rh2m = 0 if rh2m==-99
replace srad = 0 if srad ==-99
replace wind = 0 if wind ==-99
replace tdew = 0 if tdew == -99

egen t2mmean = mean(t2m), by(PKAB weyr)

* collapse by PKAB and wyer, and prepare to merge with SI 
ren weyr year

keep if year >=1997 & year <= 2012

collapse t2mmean _X _Y _ID, by (PKAB year)

reshape wide t2mmean _X _Y, i(PKAB) j(year) 

gen Dt2m_1297 = t2mmean2012 - t2mmean1997

egen t2mmean = rowmean(t2mmean*)

egen _X = rowmean(_X*)
egen _Y = rowmean(_Y*)

ren _X Long
ren _Y Lat

keep PKAB Dt2m_1297- Lat

save "${otherdata}GISregionaltemperature.dta", replace

export delimited using "${rawother}Regional_t2m.csv", replace


***
***
*Figure 4
use "${mastersi}\si_panel.dta", clear

keep if year == 2012

gen prod2 = floor(prod3/10)

egen Binva4 = xtile(value_added), by(prod2) nq(4)

egen kabu_meanVArank = mean(Binva4), by(kabucode)

collapse kabu_meanVArank, by(kabucode)

ren kabucode PKABCODE

sort PKABCODE

destring PKAB, replace

merge 1:1 PKABCODE using "${otherdata}GISregionaltemperature.dta"

keep if _m ==3

drop _m

drop Dt2m t2m

export delimited using "${rawother}Regional_VArank.csv", replace


***
***
*Table 1
use "${data}analysis_exit0112.dta", clear

gen pdty = value_added/num_workers
egen z2pdty = std(pdty)
egen z2pdty2001 = std(BinPdty3_initialprod2)

egen z2VA = std(value_added)
egen z2Sales = std(income_goodsProd)
egen z2Exporter = std(exporter)

gen CaptoProd = cap_est/laborCnt_prod
egen z2CapPrd = std(CaptoProd)

gen NPtoPrd = labor_nonprod/laborCnt_prod
egen z2NpPrd = std(NPtoPrd)

gen LaborIntensity = wages/value_added
egen z2LabInt = std(LaborIntensity)

gen wagerate = wages/num_workers
egen z2Wgrate = std(wagerate)

eststo clear

foreach var in VA Sales Exporter CapPrd NpPrd LabInt Wgrate{
	
eststo g`var': reg z2pdty z2`var', cluster(PSID)
eststo m`var': areg z2pdty z2`var', abs(prod2) cluster (PSID)
eststo r`var': reg z2pdty2001 z2`var', cluster(PSID)
	
}

esttab g* m* r*, starlevels(* 0.10 ** 0.05 *** 0.01) cells(b(star fmt(4)) se(par fmt(3))) mlabel() dropped(" ")  noomit  gaps

esttab g* m* r* using "${tables}table1.csv", starlevels(* 0.10 ** 0.05 *** 0.01) cells(b(star fmt(4)) se(par fmt(3))) mlabel() dropped(" ")  noomit  gaps


